<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
	<link rel="stylesheet" type="text/css" href="../scripts/ext-3.3.1/resources/css/ext-all.css"/>
	<link rel="stylesheet" type="text/css" href="../styles/global.css"/>
	<script type="text/javascript" src="../scripts/ext-3.3.1/adapter/ext/ext-base.js"></script>
	<script type="text/javascript" src="../scripts/ext-3.3.1/ext-all.js"></script>
	<script type="text/javascript" src="../scripts/extjs-plus.js"></script>
	<title></title>
	<script type="text/javascript">
		Ext.onReady(function(){
			var sm = new Ext.grid.CheckboxSelectionModel();
			var options = {
				title:"定时任务",
				cm:new Ext.grid.ColumnModel([
					new Ext.grid.RowNumberer(),
					sm,
					{header:"ID",width:150,sortable:true,dataIndex:"jobId",hidden:true},
					{header:"名称",width:150,sortable:true,dataIndex:"jobTitle",id:"jobTitle"},
					{header:"定时器",width:180,sortable:true,dataIndex:"cronExpression"},
		           	{header:"上次执行时间",width:180,sortable:true,dataIndex:"previousFireTime"},
		           	{header:"下次执行时间",width:180,sortable:true,dataIndex:"nextFireTime",defaultSortable:true},
		           	{header:"状态",width:100,sortable:true,dataIndex:"status"}
		        ]),
				record:new Ext.data.Record.create([
	               	{name:"index",type:"int"},
	            	{name:"jobId",type:"string"},
	            	{name:"jobTitle",type:"string"},
	            	{name:"cronExpression",type:"string"},
	             	{name:"previousFireTime",type:"string"},
	            	{name:"nextFireTime",type:"string"},,
	             	{name:"status",type:"string"}
				]),
	            url:"<s:url action='admin/schedule_query'/>",
	            start:0,
	            limit:999,
	            sm:sm,
	            remoteSort:true,
	            autoExpandColumnId:"jobTitle",
	            buttonAlign:"center",
	            showBottomBar:true,
	            tbar:[{
	            	text:"删除",
	            	icon:"../scripts/ext-3.3.1/resources/images/default/dd/drop-delete.gif",
	            	handler:function(){
	    				var rsm = grid.getSelectionModel();
	    				var view = grid.getView();
	    				var store = grid.getStore();
	    				for(var i=0;i<view.getRows().length;i++){
	    					if(rsm.isSelected(i)){
	    						var jobId = store.getAt(i).get("jobId");
	    						Ext.Ajax.request({
	    							url:"<s:url action='admin/schedule_unschedule'/>",
	    							success:function(response,config){
	    								if(response.responseText=="\"1\""){
	    									grid.store.reload();
	    								}else{
	    									Ext.Msg.alert("提示",response.responseText);
	    								}
	    							},
	    							failure:function(response,config){
	    								Ext.Msg.alert("提示","启动失败！");
	    							},
	    							method:"post",
	    							params:{jobId:jobId}
	    						});
	    					}
	    				}
	    			}
	            },{
	            	text:"添加",
	            	icon:"../scripts/ext-3.3.1/resources/images/default/dd/drop-add.gif",
	            	handler:function(){
	    				var store = new Ext.data.Store({
	    					proxy:new Ext.data.HttpProxy({
	    						url:"<s:url action="admin/schedule_jobClassNames"/>"
	    					}),
	    					reader: new Ext.data.JsonReader(
	    						{
	    							root:"root"
	    						},
	    						[
	    							{name:"value",mapping:"value"},
	    							{name:"label",mapping:"label"}
	    						]
	    					)
	    				});
	    				
	    				var f = new Ext.form.FormPanel({
	    					labelAlign:"right",
	    					frame:true,
	    					region:"center",
	    					items:[
	    						{
	    							xtype:"combo",
	    							fieldLabel:"任务名称",
	    							name:"jobId",
	    							store:store,
	    							editable:false,
	    							hiddenName:"jobId",
	    							triggerAction:"all",
	    							valueField:"value",
	    							displayField:"label",
	    							loadingText:"加载中。。。",
	    							mode:"remote",
	    							width:250,
	    							emptyText:"请选择",
	    							selectOnFocus:true
	    						},{
	    							xtype:"textfield",
	    							name:"cronExpression",
	    							width:250,
	    							allowBlank:false,
	    							fieldLabel:"定时器"
	    						}						  
	    					]
	    				});
	    				var win = new Ext.Window({
	    					title:"添加",
	    					width:450,
	    					modal:true,
	    					height:200,
	    					layout:"border",
	    					buttonAlign:"center",
	    					items:[f],
	    					buttons:[
	    						{
	    							text:"确定",
	    							handler:function(){
	    								var jobId = f.getForm().findField("jobId").getValue();
	    								var cronExpression = f.getForm().findField("cronExpression").getValue();
	    								Ext.Ajax.request({
	    									url:"<s:url action='admin/schedule_schedule'/>",
	    									success:function(response,config){
	    										if(response.responseText=="\"1\""){
	    											win.hide();
	    											grid.store.reload();
	    										}else{
	    											Ext.Msg.alert("提示",response.responseText);
	    										}
	    									},
	    									failure:function(response,config){
	    										Ext.Msg.alert("提示","添加失败！");
	    									},
	    									method:"post",
	    									params:{jobId:jobId,cronExpression:cronExpression}
	    								});
	    							}
	    						},{
	    							text:"取消",
	    							handler:function(){
	    								win.hide();
	    							}
	    						}
	    					]
	    				});
	    				win.show();
	    			}
	            },{
	            	text:"刷新",
	            	icon:"../scripts/ext-3.3.1/resources/images/default/grid/refresh.gif",
	            	handler:function(){
	    				grid.getStore().reload();
	    			}
	            },{
	            	text:"启动",
	            	icon:"../scripts/ext-3.3.1/resources/images/default/dd/drop-yes.gif",
	            	handler:function(){
	    				var rsm = grid.getSelectionModel();
	    				var view = grid.getView();
	    				var store = grid.getStore();
	    				for(var i=0;i<view.getRows().length;i++){
	    					if(rsm.isSelected(i)){
	    						var jobId = store.getAt(i).get("jobId");
	    						Ext.Ajax.request({
	    							url:"<s:url action='admin/schedule_start'/>",
	    							success:function(response,config){
	    								if(response.responseText=="\"1\""){
	    									grid.store.reload();
	    								}else{
	    									Ext.Msg.alert("提示",response.responseText);
	    								}
	    							},
	    							failure:function(response,config){
	    								Ext.Msg.alert("提示","启动失败！");
	    							},
	    							method:"post",
	    							params:{jobId:jobId}
	    						});
	    					}
	    				}
	    			}
	            }]
			};
			
			var grid = create_gridpanel(options);
			grid.render("data");
		});
	</script>
</head>
<body>
<div id="data"></div>
<div id="tj"></div>
</body>
</html>